"""
Phase 7: Age-Technology Interaction
=====================================
Motivated by Crouzet, Ghosh, Gupta & Mezzanotti (2026): younger populations
adopt technology faster (India mobile payments).

Tests:
  1. Z₁ directly predicts technology adoption (internet, mobile, R&D/GDP)
  2. Z₁ × technology interaction on capital intensity and labor productivity
  3. Income-conditioned tests (low/mid/high terciles)
  4. 5yr lag and first-difference robustness

Output: phase7_age_technology.md
"""

import pandas as pd
import numpy as np
from pathlib import Path
import sys
import warnings
warnings.filterwarnings('ignore')

PROJECT_DIR = Path("/mnt/c/demographics_capital_flows/automation")
ROOT_DIR = PROJECT_DIR.parent
sys.path.insert(0, str(ROOT_DIR / "multilateral" / "src"))
from model import PanelGLS

DATA = PROJECT_DIR / "data" / "processed"
OUT = PROJECT_DIR / "output" / "tables"
OUT.mkdir(parents=True, exist_ok=True)

OECD = [
    'AUS', 'AUT', 'BEL', 'CAN', 'CHL', 'COL', 'CRI', 'CZE', 'DNK', 'EST',
    'FIN', 'FRA', 'DEU', 'GRC', 'HUN', 'ISL', 'IRL', 'ISR', 'ITA', 'JPN',
    'KOR', 'LVA', 'LTU', 'LUX', 'MEX', 'NLD', 'NZL', 'NOR', 'POL', 'PRT',
    'SVK', 'SVN', 'ESP', 'SWE', 'CHE', 'TUR', 'GBR', 'USA',
]

DEMO_VARS = ['Z_1', 'Z_2', 'Z_3']
CONTROLS = ['fiscal_bal_gdp', 'nfa_gdp_lag', 'log_rel_opw', 'kaopen']


def stars(p):
    if p < 0.01: return '***'
    if p < 0.05: return '**'
    if p < 0.1: return '*'
    return ''


def run_gls(df, y_var, x_vars, label, silent=False):
    cols = [y_var] + x_vars + ['iso3', 'year']
    sub = df[cols].dropna()
    if len(sub) < 50:
        if not silent:
            print(f"  SKIP {label}: only {len(sub)} obs")
        return None

    gls = PanelGLS()
    gls.fit(sub[y_var].values, sub[x_vars].values,
            sub['iso3'].values, sub['year'].values)

    result = {
        'label': label, 'dep_var': y_var,
        'n_obs': gls.n_obs, 'n_countries': gls.n_countries,
        'r_squared': gls.r_squared, 'rho': gls.rho,
    }
    for i, name in enumerate(x_vars):
        result[f'{name}_coef'] = gls.beta[i]
        result[f'{name}_se'] = gls.se[i]
        result[f'{name}_p'] = gls.pvalues[i]

    if not silent:
        print(f"  {label}: N={gls.n_obs}, {gls.n_countries}c, R²={gls.r_squared:.4f}")
        for v in DEMO_VARS:
            if f'{v}_coef' in result:
                print(f"    {v}: {result[f'{v}_coef']:.2f}{stars(result[f'{v}_p'])} "
                      f"(se={result[f'{v}_se']:.2f}, p={result[f'{v}_p']:.4f})")

    return result


def download_tech_indicators():
    """Download internet, mobile, R&D from WDI via wbgapi."""
    cache = DATA / "wdi_tech.csv"
    if cache.exists():
        print(f"  Using cached WDI tech data: {cache}")
        return pd.read_csv(cache)

    print("  Downloading WDI technology indicators via wbgapi...")
    import wbgapi as wb

    indicators = {
        'IT.NET.USER.ZS': 'internet_users_pct',      # Internet users (% population)
        'IT.CEL.SETS.P2': 'mobile_subs_per100',       # Mobile subscriptions per 100
        'GB.XPD.RSDV.GD.ZS': 'rd_gdp',               # R&D expenditure % GDP
        'IP.PAT.RESD': 'patent_residents',            # Patent applications, residents
    }

    frames = []
    for code, name in indicators.items():
        try:
            print(f"    Fetching {name} ({code})...")
            data = wb.data.DataFrame(code, time=range(1990, 2025), labels=False)
            data = data.reset_index()
            data = data.melt(id_vars=['economy'], var_name='year', value_name=name)
            data['year'] = data['year'].str.replace('YR', '').astype(int)
            data = data.rename(columns={'economy': 'iso3'})
            frames.append(data)
        except Exception as e:
            print(f"    Warning: Could not get {code}: {e}")

    if not frames:
        return None

    df = frames[0]
    for f in frames[1:]:
        df = df.merge(f, on=['iso3', 'year'], how='outer')

    df.to_csv(cache, index=False)
    print(f"  Saved: {cache} ({df.shape})")
    return df


def main():
    print("=" * 70)
    print("PHASE 7: AGE-TECHNOLOGY INTERACTION")
    print("Motivated by Crouzet, Ghosh, Gupta & Mezzanotti (2026)")
    print("=" * 70)

    # Load automation panel
    df = pd.read_csv(DATA / "automation_panel.csv")
    df = df[df['year'] <= 2024].copy()
    print(f"Automation panel: {len(df):,} obs, {df['iso3'].nunique()} countries")

    # Download and merge tech indicators
    print("\n[1] Getting technology indicators ...")
    tech = download_tech_indicators()
    if tech is not None:
        df = df.merge(tech, on=['iso3', 'year'], how='left')
        for v in ['internet_users_pct', 'mobile_subs_per100', 'rd_gdp', 'patent_residents']:
            if v in df.columns:
                n = df[v].notna().sum()
                nc = df[df[v].notna()]['iso3'].nunique()
                print(f"  {v}: {n:,} obs, {nc} countries")

    # Log-transform patents (highly skewed)
    if 'patent_residents' in df.columns:
        df['log_patents'] = np.log1p(df['patent_residents'])

    controls = [c for c in CONTROLS if c in df.columns and df[c].notna().sum() > 200]
    base_vars = DEMO_VARS + controls

    # Create income terciles
    gdp_median = df.groupby('iso3')['gdp_pc_ppp'].median()
    terciles = gdp_median.quantile([0.33, 0.67])
    low_inc = gdp_median[gdp_median <= terciles[0.33]].index
    mid_inc = gdp_median[(gdp_median > terciles[0.33]) & (gdp_median <= terciles[0.67])].index
    high_inc = gdp_median[gdp_median > terciles[0.67]].index
    print(f"\n  Income terciles: low={len(low_inc)}, mid={len(mid_inc)}, high={len(high_inc)}")

    doc_lines = []
    doc_lines.append("# Age-Technology Interaction Tests")
    doc_lines.append("")
    doc_lines.append("*Motivated by Crouzet, Ghosh, Gupta & Mezzanotti (2026), who show "
                     "younger populations adopt technology faster at the micro level "
                     "(India mobile payments). Tests whether the demographic effect on "
                     "capital intensity and labor productivity is amplified in "
                     "high-connectivity settings.*")
    doc_lines.append("")

    # ══════════════════════════════════════════════════════════════════
    # TEST 1: DEMOGRAPHICS PREDICT TECHNOLOGY ADOPTION
    # ══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 60)
    print("TEST 1: DEMOGRAPHICS → TECHNOLOGY ADOPTION")
    print("=" * 60)
    doc_lines.append("## Test 1: Do Demographics Predict Technology Adoption?")
    doc_lines.append("")
    doc_lines.append("If aging populations adopt technology more slowly (Crouzet et al.), "
                     "Z₁ should negatively predict internet penetration, mobile subscriptions, "
                     "and R&D spending.")
    doc_lines.append("")

    tech_dvs = []
    for v in ['internet_users_pct', 'mobile_subs_per100', 'rd_gdp', 'log_patents']:
        if v in df.columns and df[v].notna().sum() > 200:
            tech_dvs.append(v)

    t1_results = []
    for dv in tech_dvs:
        print(f"\n  --- {dv} ---")
        r = run_gls(df, dv, base_vars, f'Full: {dv}')
        if r: t1_results.append(r)

        # OECD
        df_oecd = df[df['iso3'].isin(OECD)]
        r_oecd = run_gls(df_oecd, dv, base_vars, f'OECD: {dv}')
        if r_oecd: t1_results.append(r_oecd)

        # Non-OECD
        df_nonoecd = df[~df['iso3'].isin(OECD)]
        r_nonoecd = run_gls(df_nonoecd, dv, base_vars, f'Non-OECD: {dv}')
        if r_nonoecd: t1_results.append(r_nonoecd)

    # Write table
    doc_lines.append("| Model | Z₁ | se | p | Z₂ | p | N | Countries | R² |")
    doc_lines.append("|:--|--:|--:|--:|--:|--:|--:|--:|--:|")
    for r in t1_results:
        z1c = r.get('Z_1_coef', np.nan)
        z1s = r.get('Z_1_se', np.nan)
        z1p = r.get('Z_1_p', np.nan)
        z2c = r.get('Z_2_coef', np.nan)
        z2p = r.get('Z_2_p', np.nan)
        doc_lines.append(f"| {r['label']} | {z1c:.1f}{stars(z1p)} | {z1s:.1f} | "
                        f"{z1p:.3f} | {z2c:.1f} | {z2p:.3f} | "
                        f"{r['n_obs']} | {r['n_countries']} | {r['r_squared']:.3f} |")
    doc_lines.append("")

    # ══════════════════════════════════════════════════════════════════
    # TEST 2: Z × TECHNOLOGY INTERACTION ON CAPITAL INTENSITY
    # ══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 60)
    print("TEST 2: Z × TECHNOLOGY → CAPITAL INTENSITY & PRODUCTIVITY")
    print("=" * 60)
    doc_lines.append("## Test 2: Z₁ × Technology Interaction on Capital Intensity and Productivity")
    doc_lines.append("")
    doc_lines.append("Does high technology adoption amplify or dampen the demographic effect "
                     "on capital intensity? If younger populations adopt technology faster AND "
                     "technology substitutes for labor, then Z₁ × internet should be positive "
                     "(aging + low tech = less automation).")
    doc_lines.append("")

    # Standardize tech vars for interactions
    for tv in tech_dvs:
        col_std = f'{tv}_std'
        mean_v = df[tv].mean()
        std_v = df[tv].std()
        if std_v > 0:
            df[col_std] = (df[tv] - mean_v) / std_v
        else:
            df[col_std] = 0

    outcome_vars = ['capital_per_worker', 'log_labor_productivity']
    outcome_vars = [v for v in outcome_vars if v in df.columns]

    t2_results = []
    for outcome in outcome_vars:
        for tv in tech_dvs:
            tv_std = f'{tv}_std'
            if tv_std not in df.columns:
                continue
            df['Z_1_x_tech'] = df['Z_1'] * df[tv_std]

            interact_vars = base_vars + [tv_std, 'Z_1_x_tech']
            label = f'{outcome} × {tv.replace("_pct","").replace("_per100","").replace("_users","")}'
            r = run_gls(df, outcome, interact_vars, label)
            if r:
                # Store which tech var was used
                r['tech_var'] = tv
                r['outcome'] = outcome
                t2_results.append(r)

    doc_lines.append("| Outcome | Tech moderator | Z₁ | p | Z₁×Tech | se | p | N | R² |")
    doc_lines.append("|:--|:--|--:|--:|--:|--:|--:|--:|--:|")
    for r in t2_results:
        z1c = r.get('Z_1_coef', np.nan)
        z1p = r.get('Z_1_p', np.nan)
        intc = r.get('Z_1_x_tech_coef', np.nan)
        ints = r.get('Z_1_x_tech_se', np.nan)
        intp = r.get('Z_1_x_tech_p', np.nan)
        doc_lines.append(f"| {r['outcome']} | {r['tech_var']} | "
                        f"{z1c:.1f}{stars(z1p)} | {z1p:.3f} | "
                        f"{intc:.1f}{stars(intp)} | {ints:.1f} | {intp:.3f} | "
                        f"{r['n_obs']} | {r['r_squared']:.3f} |")
    doc_lines.append("")

    # ══════════════════════════════════════════════════════════════════
    # TEST 3: INCOME-CONDITIONED TECHNOLOGY INTERACTIONS
    # ══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 60)
    print("TEST 3: INCOME-CONDITIONED TESTS")
    print("=" * 60)
    doc_lines.append("## Test 3: Income-Conditioned Technology Interactions")
    doc_lines.append("")
    doc_lines.append("Crouzet et al.'s India finding suggests the age-technology nexus "
                     "may be strongest in developing countries where technology leapfrogging "
                     "is possible. Test Z₁ × internet by income tercile.")
    doc_lines.append("")

    # Use internet as main tech var (best coverage)
    main_tech = 'internet_users_pct'
    if main_tech in df.columns and 'internet_users_pct_std' in df.columns:
        df['Z_1_x_internet'] = df['Z_1'] * df['internet_users_pct_std']

        t3_results = []
        for tercile_name, tercile_isos in [('Low income', low_inc),
                                            ('Mid income', mid_inc),
                                            ('High income', high_inc)]:
            df_terc = df[df['iso3'].isin(tercile_isos)]
            for outcome in outcome_vars:
                interact_vars = base_vars + ['internet_users_pct_std', 'Z_1_x_internet']
                r = run_gls(df_terc, outcome, interact_vars,
                           f'{tercile_name}: {outcome}')
                if r:
                    r['tercile'] = tercile_name
                    r['outcome'] = outcome
                    t3_results.append(r)

        doc_lines.append("| Tercile | Outcome | Z₁ | p | Z₁×Internet | p | N | Countries |")
        doc_lines.append("|:--|:--|--:|--:|--:|--:|--:|--:|")
        for r in t3_results:
            z1c = r.get('Z_1_coef', np.nan)
            z1p = r.get('Z_1_p', np.nan)
            intc = r.get('Z_1_x_internet_coef', np.nan)
            intp = r.get('Z_1_x_internet_p', np.nan)
            doc_lines.append(f"| {r['tercile']} | {r['outcome']} | "
                            f"{z1c:.1f}{stars(z1p)} | {z1p:.3f} | "
                            f"{intc:.1f}{stars(intp)} | {intp:.3f} | "
                            f"{r['n_obs']} | {r['n_countries']} |")
        doc_lines.append("")

    # ══════════════════════════════════════════════════════════════════
    # TEST 4: TECHNOLOGY AS MEDIATOR (ATTENUATION TEST)
    # ══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 60)
    print("TEST 4: TECHNOLOGY AS MEDIATOR")
    print("=" * 60)
    doc_lines.append("## Test 4: Technology Adoption as Mediator")
    doc_lines.append("")
    doc_lines.append("If demographics → technology → capital intensity, then controlling for "
                     "internet/mobile should attenuate Z₁ on capital intensity.")
    doc_lines.append("")

    t4_results = []
    main_outcome = 'capital_per_worker'
    if main_outcome in df.columns:
        # Baseline without tech
        r_base = run_gls(df, main_outcome, base_vars, 'Baseline (no tech)')
        if r_base: t4_results.append(r_base)

        # With internet control
        if main_tech in df.columns:
            vars_w_internet = base_vars + [main_tech]
            r_w_int = run_gls(df, main_outcome, vars_w_internet, '+ internet_users')
            if r_w_int: t4_results.append(r_w_int)

        # With mobile control
        if 'mobile_subs_per100' in df.columns:
            vars_w_mobile = base_vars + ['mobile_subs_per100']
            r_w_mob = run_gls(df, main_outcome, vars_w_mobile, '+ mobile_subs')
            if r_w_mob: t4_results.append(r_w_mob)

        # With R&D control
        if 'rd_gdp' in df.columns:
            vars_w_rd = base_vars + ['rd_gdp']
            r_w_rd = run_gls(df, main_outcome, vars_w_rd, '+ R&D/GDP')
            if r_w_rd: t4_results.append(r_w_rd)

        # All tech controls
        all_tech = [v for v in [main_tech, 'mobile_subs_per100', 'rd_gdp']
                    if v in df.columns]
        if all_tech:
            vars_all = base_vars + all_tech
            r_all = run_gls(df, main_outcome, vars_all, '+ all tech controls')
            if r_all: t4_results.append(r_all)

    doc_lines.append("| Model | Z₁ | se | p | Attenuation | N | R² |")
    doc_lines.append("|:--|--:|--:|--:|--:|--:|--:|")
    base_z1 = t4_results[0]['Z_1_coef'] if t4_results else 0
    for r in t4_results:
        z1c = r.get('Z_1_coef', np.nan)
        z1s = r.get('Z_1_se', np.nan)
        z1p = r.get('Z_1_p', np.nan)
        if r == t4_results[0]:
            att = "—"
        else:
            att_pct = (1 - z1c/base_z1) * 100 if base_z1 != 0 else 0
            att = f"{att_pct:.0f}%"
        doc_lines.append(f"| {r['label']} | {z1c:.1f}{stars(z1p)} | {z1s:.1f} | "
                        f"{z1p:.3f} | {att} | {r['n_obs']} | {r['r_squared']:.3f} |")
    doc_lines.append("")

    if len(t4_results) >= 2:
        base_z1 = t4_results[0]['Z_1_coef']
        last_z1 = t4_results[-1]['Z_1_coef']
        att_pct = (1 - last_z1/base_z1) * 100 if base_z1 != 0 else 0
        if abs(att_pct) < 20:
            doc_lines.append(f"**Assessment:** Technology controls attenuate Z₁ by only "
                            f"{abs(att_pct):.0f}% — technology adoption does NOT mediate "
                            f"the demographic effect on capital intensity.")
        elif att_pct > 0:
            doc_lines.append(f"**Assessment:** Technology controls attenuate Z₁ by "
                            f"{att_pct:.0f}% — partial mediation through technology adoption.")
        else:
            doc_lines.append(f"**Assessment:** Technology controls AMPLIFY Z₁ by "
                            f"{abs(att_pct):.0f}% (negative attenuation) — technology "
                            f"suppresses the demographic signal, similar to the S-I suppression "
                            f"pattern in Paper 13.")
    doc_lines.append("")

    # ══════════════════════════════════════════════════════════════════
    # TEST 5: LAG AND FIRST-DIFFERENCE ROBUSTNESS
    # ══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 60)
    print("TEST 5: LAG AND FIRST-DIFFERENCE")
    print("=" * 60)
    doc_lines.append("## Test 5: Lag and First-Difference Robustness")
    doc_lines.append("")

    t5_results = []
    if main_tech in df.columns:
        # 5yr lag of tech
        df = df.sort_values(['iso3', 'year'])
        df['internet_lag5'] = df.groupby('iso3')[main_tech].shift(5)
        df['Z_1_x_internet_lag5'] = df['Z_1'] * df['internet_lag5']

        # Lag interaction
        lag_vars = base_vars + ['internet_lag5', 'Z_1_x_internet_lag5']
        for outcome in outcome_vars:
            r = run_gls(df, outcome, lag_vars, f'5yr lag internet: {outcome}')
            if r: t5_results.append(r)

        # First difference of tech
        df['d_internet'] = df.groupby('iso3')[main_tech].diff()
        df['d_capital_per_worker'] = df.groupby('iso3')['capital_per_worker'].diff()

        if 'd_capital_per_worker' in df.columns and 'd_internet' in df.columns:
            d_vars = DEMO_VARS + [c for c in controls if c in df.columns] + ['d_internet']
            r_fd = run_gls(df, 'd_capital_per_worker', d_vars, 'First-diff: ΔK/L ~ Z + Δinternet')
            if r_fd: t5_results.append(r_fd)

    doc_lines.append("| Model | Z₁ | p | Interaction | p | N | R² |")
    doc_lines.append("|:--|--:|--:|--:|--:|--:|--:|")
    for r in t5_results:
        z1c = r.get('Z_1_coef', np.nan)
        z1p = r.get('Z_1_p', np.nan)
        # Check for interaction term
        int_key = None
        for k in r:
            if 'x_internet' in k and k.endswith('_coef'):
                int_key = k.replace('_coef', '')
                break
        if int_key:
            intc = r.get(f'{int_key}_coef', np.nan)
            intp = r.get(f'{int_key}_p', np.nan)
            doc_lines.append(f"| {r['label']} | {z1c:.1f}{stars(z1p)} | {z1p:.3f} | "
                            f"{intc:.1f}{stars(intp)} | {intp:.3f} | "
                            f"{r['n_obs']} | {r['r_squared']:.3f} |")
        else:
            doc_lines.append(f"| {r['label']} | {z1c:.1f}{stars(z1p)} | {z1p:.3f} | "
                            f"— | — | {r['n_obs']} | {r['r_squared']:.3f} |")
    doc_lines.append("")

    # ══════════════════════════════════════════════════════════════════
    # TEST 6: CROUZET DIRECT TEST — DEMOGRAPHICS → CA VIA TECHNOLOGY
    # ══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 60)
    print("TEST 6: DEMOGRAPHICS → CA, MEDIATED BY TECHNOLOGY")
    print("=" * 60)
    doc_lines.append("## Test 6: Demographics → CA, Technology Channel")
    doc_lines.append("")
    doc_lines.append("Crouzet et al. argue technology adoption affects trade competitiveness. "
                     "If Z → technology → trade → CA, then controlling for internet should "
                     "attenuate Z₁ on CA/GDP.")
    doc_lines.append("")

    t6_results = []
    if 'ca_gdp' in df.columns:
        r_base_ca = run_gls(df, 'ca_gdp', base_vars, 'CA baseline')
        if r_base_ca: t6_results.append(r_base_ca)

        if main_tech in df.columns:
            r_ca_int = run_gls(df, 'ca_gdp', base_vars + [main_tech], 'CA + internet')
            if r_ca_int: t6_results.append(r_ca_int)

        if 'rd_gdp' in df.columns:
            r_ca_rd = run_gls(df, 'ca_gdp', base_vars + ['rd_gdp'], 'CA + R&D')
            if r_ca_rd: t6_results.append(r_ca_rd)

    doc_lines.append("| Model | Z₁ | se | p | Attenuation | N | R² |")
    doc_lines.append("|:--|--:|--:|--:|--:|--:|--:|")
    base_ca_z1 = t6_results[0]['Z_1_coef'] if t6_results else 0
    for r in t6_results:
        z1c = r.get('Z_1_coef', np.nan)
        z1s = r.get('Z_1_se', np.nan)
        z1p = r.get('Z_1_p', np.nan)
        if r == t6_results[0]:
            att = "—"
        else:
            att = f"{(1 - z1c/base_ca_z1)*100:.0f}%" if base_ca_z1 != 0 else "—"
        doc_lines.append(f"| {r['label']} | {z1c:.1f}{stars(z1p)} | {z1s:.1f} | "
                        f"{z1p:.3f} | {att} | {r['n_obs']} | {r['r_squared']:.3f} |")
    doc_lines.append("")

    # ══════════════════════════════════════════════════════════════════
    # SYNTHESIS
    # ══════════════════════════════════════════════════════════════════
    doc_lines.append("## Synthesis")
    doc_lines.append("")

    # Count significant results
    sig_adoption = sum(1 for r in t1_results
                       if r and r.get('Z_1_p', 1) < 0.05 and 'Full' in r['label'])
    total_adoption = sum(1 for r in t1_results if r and 'Full' in r['label'])

    sig_interact = sum(1 for r in t2_results
                       if r and r.get('Z_1_x_tech_p', 1) < 0.05)
    total_interact = len(t2_results)

    doc_lines.append(f"1. **Demographics → technology adoption:** {sig_adoption}/{total_adoption} "
                    f"technology indicators show significant Z₁ effects in the full sample.")
    doc_lines.append("")

    doc_lines.append(f"2. **Z₁ × technology interactions:** {sig_interact}/{total_interact} "
                    f"interaction tests are significant.")
    doc_lines.append("")

    if t4_results and len(t4_results) >= 2:
        att = (1 - t4_results[-1]['Z_1_coef']/t4_results[0]['Z_1_coef']) * 100
        doc_lines.append(f"3. **Mediation:** Technology controls attenuate Z₁ on capital "
                        f"intensity by {att:.0f}%.")
    doc_lines.append("")

    doc_lines.append("### Connection to Crouzet et al. (2026)")
    doc_lines.append("")
    doc_lines.append("Crouzet et al. show that younger populations adopt mobile payments "
                     "faster in India, suggesting technology adoption as a channel through "
                     "which demographics affect economic outcomes. Our tests examine whether "
                     "this micro-level finding aggregates to the macro level: do countries "
                     "with younger populations have higher technology adoption, and does "
                     "technology moderate the demographic effect on capital accumulation? "
                     "The results inform whether the automation paper's K/L deepening "
                     "finding operates partly through a technology adoption channel.")
    doc_lines.append("")
    doc_lines.append("---")
    doc_lines.append(f"*Generated: March 8, 2026. Panel: automation_panel.csv + WDI tech "
                    f"indicators ({df['iso3'].nunique()} countries, 1990-2024).*")

    # Write document
    out_path = OUT / "phase7_age_technology.md"
    out_path.write_text('\n'.join(doc_lines), encoding='utf-8')
    print(f"\nSaved: {out_path}")
    print(f"Size: {out_path.stat().st_size / 1024:.0f} KB")


if __name__ == "__main__":
    main()
